package com.hustzp.com.xichuangzhu.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FunctionCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

import java.util.HashMap;
import java.util.Map;

public class ResetPassWord extends XCZBaseFragmentActivity {
    private EditText password;
    TextView tMiddle, tRight;
    ImageView tLeftImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pass_word);
        initView();
    }

    public void initView() {
        password = (EditText) findViewById(R.id.password);
        tMiddle = (TextView) findViewById(R.id.middle_text);
        tRight = (TextView) findViewById(R.id.right_text);
        tRight.setVisibility(View.GONE);
        tLeftImage = (ImageView) findViewById(R.id.left_Image);
        tMiddle.setText(getResources().getString(R.string.reset_password));
        tLeftImage.setOnClickListener(viewOnclick);
        findViewById(R.id.send_button).setOnClickListener(viewOnclick);
    }

    public View.OnClickListener viewOnclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int viewID = v.getId();
            switch (viewID) {
                case R.id.left_Image:
                    ResetPassWord.this.finish();
                    break;
                case R.id.send_button:
                    resetPassWord();
                    break;
            }
        }
    };

    private void resetPassWord() {
        final String passwordS = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordS) | passwordS.length() < 6) {
            DialogFactory.hintDialog(this, getResources().getString(R.string.reset_password_no_nolone));
            return;
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("phoneNumber", AVUser.getCurrentUser().getMobilePhoneNumber());
        map.put("password", AVUser.getCurrentUser().getMobilePhoneNumber());//
        AVCloud.callFunctionInBackground("resetPassword", map, new FunctionCallback<Object>() {
            @Override
            public void done(Object o, AVException e) {
                if (e == null) {
                    Intent intent = new Intent(ResetPassWord.this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    // successfully
                } else {
                    DialogFactory.hintDialog(ResetPassWord.this, e.getMessage());
                    // failed
                }
            }
        });
    }
}
